package com.lzb.watermarkcamera.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.TimeUtils;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.lzb.watermarkcamera.AppConfig;
import com.lzb.watermarkcamera.R;
import com.lzb.watermarkcamera.basic.dialog.BaseSmartDialog;
import com.lzb.watermarkcamera.databinding.DialogSelHourBinding;
import com.lzb.watermarkcamera.databinding.DialogSelTimeBinding;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class SelHourDialog extends BaseSmartDialog<DialogSelHourBinding> implements View.OnClickListener {

    private String title;

    private List<String> listYear = new ArrayList<>();
    private List<String> listMonth = new ArrayList<>();
    private List<String> listDay = new ArrayList<>();
    private List<String> listHour = new ArrayList<>();
    private List<String> listMinute = new ArrayList<>();

    public void setTitle(String title) {
        this.title = title;
    }

    public SelHourDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sel_hour;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.wheelView1.setDividerColor(Color.parseColor("#00000000"));
        mDataBinding.wheelView1.setTextColorCenter(Color.parseColor("#202739"));
        mDataBinding.wheelView1.setTextColorOut(Color.parseColor("#80202739"));
        mDataBinding.wheelView1.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView1.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView1.setCurrentItem(0);
        mDataBinding.wheelView1.setItemsVisibleCount(5);
        mDataBinding.wheelView1.setCyclic(false);
        mDataBinding.wheelView1.setTextSize(16);
        mDataBinding.wheelView2.setDividerColor(Color.parseColor("#00000000"));
        mDataBinding.wheelView2.setTextColorCenter(Color.parseColor("#202739"));
        mDataBinding.wheelView2.setTextColorOut(Color.parseColor("#80202739"));
        mDataBinding.wheelView2.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView2.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView2.setCurrentItem(0);
        mDataBinding.wheelView2.setItemsVisibleCount(5);
        mDataBinding.wheelView2.setCyclic(false);
        mDataBinding.wheelView2.setTextSize(16);

        //设置滚轮数据
        listHour = new ArrayList<>();//时 24
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                listHour.add("0" + String.valueOf(i));
            } else {
                listHour.add(String.valueOf(i));
            }
        }
        listMinute = new ArrayList<>();//分 60
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                listMinute.add("0" + String.valueOf(i));
            } else {
                listMinute.add(String.valueOf(i));
            }
        }
        WheelAdapter adapter1 = new ArrayWheelAdapter(listHour);
        mDataBinding.wheelView1.setAdapter(adapter1);
        WheelAdapter adapter2 = new ArrayWheelAdapter(listMinute);
        mDataBinding.wheelView2.setAdapter(adapter2);


        //滚动到对应日期
        String time = AppConfig.DATE + " " + AppConfig.TIME;
        long mill = TimeUtils.string2Millis(time, new SimpleDateFormat("yyyy-MM-dd HH:mm"));
        String date = TimeUtils.millis2String(mill, new SimpleDateFormat("yyyy-MM-dd-HH-mm"));
        String[] split = date.split("-");
        int year = Integer.parseInt(split[0]);//年
        int moon = Integer.parseInt(split[1]);//月
        int day = Integer.parseInt(split[2]);//日
        int hour = Integer.parseInt(split[3]);//日
        int minute = Integer.parseInt(split[4]);//日
        mDataBinding.wheelView1.setCurrentItem(hour);
        mDataBinding.wheelView2.setCurrentItem(minute);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            dismiss();
            if (listener != null) {
                String str = listHour.get(mDataBinding.wheelView1.getCurrentItem()) + getContext().getString(R.string.hour) +
                        listMinute.get(mDataBinding.wheelView2.getCurrentItem()) + getContext().getString(R.string.minute);
                long time = TimeUtils.string2Millis(str, new SimpleDateFormat("HH时mm分"));
                listener.clickConfirm(time);
            }
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(long time);
    }
}
